package io.renren.wechat.controller;

import io.renren.common.utils.R;
import io.renren.wechat.service.contact.UserService;
import io.renren.wechat.service.suite.SuiteService;
import io.renren.wechat.service.suite.dto.AdminInfo;
import io.renren.wechat.service.suite.dto.UserDetailInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dmcp@qq.com
 * @since 2021/11/29 22:11
 */
@RequestMapping("/wechat/user")
@RestController
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    private UserService userService;
    private SuiteService suiteService;

    @RequestMapping("/openId")
    public R userId2openId(@RequestParam(value = "corpId", required = false) String corpId,
                           @RequestParam(value = "userId", required = false) String userId) {
        LOGGER.debug("query openId with userId, corpId: {}, userId: {}", corpId, userId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("corpId", corpId);
        resultMap.put("userId", userId);

        if (StringUtils.isBlank(corpId) || StringUtils.isBlank(userId)) {
            return R.ok(resultMap);
        }

        final String openId = userService.userId2openId(corpId, userId, null);
        resultMap.put("openId", openId);

        return R.ok(resultMap);
    }

    @RequestMapping("/adminList")
    public R adminList(@RequestParam(value = "corpId", required = false) String corpId,
                       @RequestParam(value = "agentId", required = false) String agentId) {
        if (StringUtils.isBlank(corpId)) {
            return R.error("corpId is required");
        }
        if (StringUtils.isBlank(agentId)) {
            return R.error("agentId is required");
        }

        final List<AdminInfo> adminList = suiteService.getAdminList(corpId, agentId, "0");

        return R.ok().put("adminList", adminList);
    }

    @RequestMapping("/detail")
    public R detail(@RequestParam(value = "corpId", required = false) String corpId,
                       @RequestParam(value = "userId", required = false) String userId) {
        if (StringUtils.isBlank(corpId)) {
            return R.error("corpId is required");
        }
        if (StringUtils.isBlank(userId)) {
            return R.error("userId is required");
        }

        final UserDetailInfo userDetailInfo = suiteService.getUserDetailInfo(corpId, userId);

        return R.ok().put("userDetailInfo", userDetailInfo);
    }


    @Autowired
    public void setSuiteService(SuiteService suiteService) {
        this.suiteService = suiteService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
